package com.czxy.enterprise.service.impl;

import com.alibaba.nacos.api.cmdb.pojo.EntityEventType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.domain.*;
import com.czxy.enterprise.feign.CandidatesFeign;
import com.czxy.enterprise.mapper.RecruitmentInfoMapper;
import com.czxy.enterprise.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.findjob.vo.BaseResult;
import com.czxy.vo.CityVO;
import com.czxy.vo.InfoVo;
import com.czxy.vo.RecruitmentInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 招聘信息表 服务实现类
 * </p>
 *
 * @author xuewen
 * @since 2022-01-25
 */
@Service
public class RecruitmentInfoServiceImpl extends ServiceImpl<RecruitmentInfoMapper, TbRecruitmentInfo> implements RecruitmentInfoService {

    @Autowired
    private OccupationService occupationService;
    @Autowired
    private WelfareService welfareService;
    @Autowired
    private CityService cityService;
    @Autowired
    private EnterpriseService enterpriseService;
    @Autowired
    private CandidatesFeign candidatesFeign;
    /**
     * @description 分页查询招聘信息列表
     * @params [current, size, recruitmentInfoVo]
     * @author 薛文
     * @date 2022/1/27 16:50
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.czxy.domain.TbRecruitmentInfo>
    */
    @Override
    public Page<TbRecruitmentInfo> condition(Integer current, Integer size, RecruitmentInfoVo recruitmentInfoVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.isNotBlank(recruitmentInfoVo.getOccupationId()),"occupation_id",recruitmentInfoVo.getOccupationId());
        queryWrapper.eq(StringUtils.isNotBlank(recruitmentInfoVo.getCityId()),"address_id",recruitmentInfoVo.getCityId());
        queryWrapper.ge(StringUtils.isNotBlank(recruitmentInfoVo.getSalary()),"min_salary",recruitmentInfoVo.getSalary());
        queryWrapper.le(StringUtils.isNotBlank(recruitmentInfoVo.getSalary()),"max_salary",recruitmentInfoVo.getSalary());
        Page<TbRecruitmentInfo> page = new Page(current,size);
        this.baseMapper.selectPage(page,queryWrapper);
        List<TbRecruitmentInfo> records = page.getRecords();
        for (TbRecruitmentInfo record : records) {
            //地址
            record.setTbCity(cityService.getById(record.getAddressId()));
            //种类
            record.setTbOccupation(occupationService.getById(record.getOccupationId()));
            //福利
            String welfareIds = record.getWelfareIds();
            String[] split = welfareIds.split(",");
            List<String> welfareIdList = Arrays.asList(split);
            List<TbWelfare> welfareList = new ArrayList<>();
            for (String s : welfareIdList) {
                if (StringUtils.isNotBlank(s)){
                    welfareList.add(welfareService.getById(s));
                }
            }
            record.setWelfares(welfareList);
             //赋值企业的数据
            TbEnterprise tbEnterprise = enterpriseService.findById(record.getEnterpriseId());
            record.setTbEnterprise(tbEnterprise);

        }
        return page;
    }
    /**
     * @description
     * @params [id]  根据id查询招聘信息
     * @author 薛文
     * @date 2022/1/27 16:50
     * @return com.czxy.domain.TbRecruitmentInfo
    */
    @Override
    public TbRecruitmentInfo findById(String id) {
        TbRecruitmentInfo tbRecruitmentInfo = this.baseMapper.selectById(id);
        if (tbRecruitmentInfo==null){
            return null;
        }
        if (tbRecruitmentInfo.getAddressId()!=null){
            tbRecruitmentInfo.setTbCity(cityService.getById(tbRecruitmentInfo.getAddressId()));
        }
        tbRecruitmentInfo.setTbOccupation(occupationService.getById(tbRecruitmentInfo.getOccupationId()));
        // 赋值福利的数据
        String welfareIds = tbRecruitmentInfo.getWelfareIds();
        String[] split = welfareIds.split(",");
        List<String> welfareIdList = Arrays.asList(split);
        List<TbWelfare> welfareList = new ArrayList<>();
        for (String s : welfareIdList) {
            if (StringUtils.isNotBlank(s)){
                welfareList.add(welfareService.getById(s));
            }
        }
        tbRecruitmentInfo.setWelfares(welfareList);
        // 赋值企业的数据
        TbEnterprise tbEnterprise = enterpriseService.findById(tbRecruitmentInfo.getEnterpriseId());
        tbRecruitmentInfo.setTbEnterprise(tbEnterprise);

        return tbRecruitmentInfo;
    }

    @Override
    public Page<TbRecruitmentInfo> condition(InfoVo infoVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.isNotBlank(infoVo.getOccupationId()),"occupation_id",infoVo.getOccupationId());
        queryWrapper.eq(StringUtils.isNotBlank(infoVo.getAddressId()),"address_id",infoVo.getAddressId());
        queryWrapper.like(StringUtils.isNotBlank(infoVo.getTitle()),"title",infoVo.getTitle());
        queryWrapper.eq(StringUtils.isNotBlank(infoVo.getEnterpriseId()),"enterprise_id",infoVo.getEnterpriseId());
        Page<TbRecruitmentInfo> page = new Page(infoVo.getCurrent(), infoVo.getSize());
        this.baseMapper.selectPage(page,queryWrapper);
        for (TbRecruitmentInfo record : page.getRecords()) {
            //地址
            record.setTbCity(cityService.getById(record.getAddressId()));
            //种类
            record.setTbOccupation(occupationService.getById(record.getOccupationId()));
            //福利
            String welfareIds = record.getWelfareIds();
            String[] split = welfareIds.split(",");
            List<String> welfareIdList = Arrays.asList(split);
            List<TbWelfare> welfareList = new ArrayList<>();
            for (String s : welfareIdList) {
                if (StringUtils.isNotBlank(s)){
                    welfareList.add(welfareService.getById(s));
                }
            }
            record.setWelfares(welfareList);
            // 赋值企业的数据
            TbEnterprise tbEnterprise = enterpriseService.findById(record.getEnterpriseId());
            record.setTbEnterprise(tbEnterprise);

        }
        return page;
    }

    @Override
    public List<TbRecruitmentInfo> findByUserId(Integer userId) {
        List<TbEnterprise> enterpriseList = enterpriseService.findByUserId(userId,1);
        List<Integer> enterpriseIdList = enterpriseList.stream().map(tbEnterprise -> tbEnterprise.getId()).collect(Collectors.toList());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("enterprise_id",enterpriseIdList);
        List<TbRecruitmentInfo> list = this.baseMapper.selectList(queryWrapper);
        for (TbRecruitmentInfo tbRecruitmentInfo : list) {
            TbEnterprise tbEnterprise = enterpriseService.findById(tbRecruitmentInfo.getEnterpriseId());
            tbRecruitmentInfo.setTbEnterprise(tbEnterprise);
            // 赋值福利的数据
            String welfareIds = tbRecruitmentInfo.getWelfareIds();
            String[] split = welfareIds.split(",");
            List<String> welfareIdList = Arrays.asList(split);
            List<TbWelfare> welfareList = new ArrayList<>();
            for (String s : welfareIdList) {
                if (StringUtils.isNotBlank(s)){
                    welfareList.add(welfareService.getById(s));
                }
            }
            tbRecruitmentInfo.setWelfares(welfareList);
        }

        return list;
    }

    @Override
    public List<CityVO> findNumByCity() {
        return this.baseMapper.findNumByCity();
    }

    @Override
    public List<TbRecruitmentInfo> notInIds(List<Integer> ids, Integer total) {
        QueryWrapper queryWrapper3 = new QueryWrapper();
        queryWrapper3.notIn(ids.size()>0,"id",ids);
        Page<TbRecruitmentInfo> page = new Page(1,total);
        this.baseMapper.selectPage(page,queryWrapper3);
        for (TbRecruitmentInfo record : page.getRecords()) {
            //地址
            record.setTbCity(cityService.getById(record.getAddressId()));
            //种类
            record.setTbOccupation(occupationService.getById(record.getOccupationId()));
            //福利
            String welfareIds = record.getWelfareIds();
            String[] split = welfareIds.split(",");
            List<String> welfareIdList = Arrays.asList(split);
            List<TbWelfare> welfareList = new ArrayList<>();
            for (String s : welfareIdList) {
                if (StringUtils.isNotBlank(s)){
                    welfareList.add(welfareService.getById(s));
                }
            }
            record.setWelfares(welfareList);
            //赋值企业的数据
            TbEnterprise tbEnterprise = enterpriseService.findById(record.getEnterpriseId());
            record.setTbEnterprise(tbEnterprise);
        }
        return page.getRecords();
    }

    @Override
    public List<TbRecruitmentInfo> recommend(Integer candidateId) {
        TbCandidates tbCandidates = candidatesFeign.getById(candidateId).getData();
        List<TbRecruitmentInfo> list = new ArrayList<>();
        if (tbCandidates.getOccupationId()!=null){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("occupation_id",tbCandidates.getOccupationId());
            queryWrapper.orderByDesc("create_time");
            Page<TbRecruitmentInfo> page = new Page(1,20);
            this.baseMapper.selectPage(page,queryWrapper);
            if (page.getRecords().size()<20){
              int total =  20-page.getRecords().size();
              QueryWrapper queryWrapper2 = new QueryWrapper<>();
              queryWrapper2.ne("occupation_id",tbCandidates.getOccupationId());
              queryWrapper2.orderByDesc("create_time");
              Page<TbRecruitmentInfo> page2  = new Page(1,total);
              this.baseMapper.selectPage(page2,queryWrapper2);
                for (int i = 0; i <page2.getRecords().size() ; i++) {
                    page.getRecords().add(page2.getRecords().get(i));
                    if (page.getRecords().size()>=20){
                        list =  page.getRecords();
                        return list;
                    }
                }
            }

        }else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.orderByDesc("create_time");
            Page<TbRecruitmentInfo> page = new Page(1,20);
            this.baseMapper.selectPage(page,queryWrapper);
            list =  page.getRecords();
        }
        for (TbRecruitmentInfo record : list) {
            //地址
            record.setTbCity(cityService.getById(record.getAddressId()));
            //种类
            record.setTbOccupation(occupationService.getById(record.getOccupationId()));
            //福利
            String welfareIds = record.getWelfareIds();
            String[] split = welfareIds.split(",");
            List<String> welfareIdList = Arrays.asList(split);
            List<TbWelfare> welfareList = new ArrayList<>();
            for (String s : welfareIdList) {
                if (StringUtils.isNotBlank(s)){
                    welfareList.add(welfareService.getById(s));
                }
            }
            record.setWelfares(welfareList);
            //赋值企业的数据
            TbEnterprise tbEnterprise = enterpriseService.findById(record.getEnterpriseId());
            record.setTbEnterprise(tbEnterprise);
        }
        return list;
    }
}
